import { createStore, Store, useStore as useVuexStore } from 'vuex';
import loginModule from './login/login';
import systemModule from './main/system/system';
import type { IRootState, IStoreType } from './type';
import { getPageListData } from '@/service/main/system/system';
const store = createStore<IRootState>({
  state() {
    return {
      entireDepartment: [],
      entireRole: []
    };
  },
  mutations: {
    changedepartmentData(state, payload) {
      state.entireDepartment = payload;
    },
    changeroleData(state, payload) {
      state.entireRole = payload;
    }
  },
  getters: {},
  actions: {
    async getPageDialogAction({ commit }) {
      /* 请求全部部门数据 */
      const departmentData = await getPageListData('/department/list', {
        offset: 0,
        size: 10
      });
      const { list: departmentList } = departmentData.data;
      /* 请求全部角色数据 */
      const roleData = await getPageListData('/role/list', {
        offset: 0,
        size: 10
      });
      const { list: roleList } = roleData.data;
      commit(`changedepartmentData`, departmentList);
      commit(`changeroleData`, roleList);
    }
  },
  modules: {
    login: loginModule,
    system: systemModule
  }
});
/* 初始化state */
export function setupStore() {
  store.dispatch('login/loginStateInit');
  store.dispatch('getPageDialogAction');
}
/* 实现调用store有类型的功能 */
export function useStore(): Store<IStoreType> {
  return useVuexStore();
}
export default store;
